package com.jimikongjian.service.dao.impl;

import com.jimikongjian.dao.IApplicationInfo;
import com.jimikongjian.models.ApplicationInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by fuxiuyang on 17/3/4.
 */
@Service("iApplicationInfoImpl")
public class IApplicationInfoImpl implements IApplicationInfo {

    @Autowired
//    @Qualifier("applicationInfoMapper")
    private IApplicationInfo iApplicationInfo;

    /**
     * 通过申请表中的id号，获取申请信息
     *
     * @param id
     * @return List<ApplicationInfo>
     */
    public List<ApplicationInfo> getApplicationListById(int id) {
        if (id > 0){
            return iApplicationInfo.getApplicationListById(id);
        }
        return null;
    }

    /**
     * 通过申请表中的id号，获取唯一的申请信息
     *
     * @param id
     * @return ApplicationInfo
     */
    public ApplicationInfo getApplicationById(int id) {
        if (id > 0){
            List<ApplicationInfo> applicationInfoList = getApplicationListById(id);
            if (applicationInfoList.size() > 0){
                return applicationInfoList.get(0);
            }
        }
        return null;
    }

    /**
     * 通过申请者的id，获取其申请消息
     *
     * @param applicatorId
     * @param start
     * @param length
     * @return List<ApplicationInfo>
     */
    public List<ApplicationInfo> getApplicationListByApplicatorId(int applicatorId, int start, int length) {
        if (applicatorId > 0 && start >= 0 && length > 0){
            return iApplicationInfo.getApplicationListByApplicatorId(applicatorId, start, length);
        }
        return null;
    }

    /**
     * 通过申请者的id，获取其申请消息
     *
     * @param applicatorId
     * @param start
     * @param length
     * @return List<ApplicationInfo>
     */
    public List<ApplicationInfo> getUnreadApplicationListByApplicatorId(int applicatorId, int start, int length) {
        if (applicatorId > 0 && start >= 0 && length > 0){
            return iApplicationInfo.getUnreadApplicationListByApplicatorId(applicatorId, start, length);
        }
        return null;
    }

    /**
     * 通过创建者的id号，获取申请创建者创建的空间的申请者的信息
     *
     * @param roomCreator
     * @param start
     * @param length
     * @return List<ApplicationInfo>
     */
    public List<ApplicationInfo> getApplicationListByRoomCreator(int roomCreator, int start, int length) {
        if (roomCreator > 0 && start >= 0 && length > 0){
            return iApplicationInfo.getApplicationListByRoomCreator(roomCreator, start, length);
        }
        return null;
    }

    /**
     * 通过创建者的id号，获取申请创建者创建的空间的申请者的信息
     *
     * @param roomCreator
     * @param start
     * @param length
     * @return List<ApplicationInfo>
     */
    public List<ApplicationInfo> getUnreadApplicationListByRoomCreator(int roomCreator, int start, int length) {
        if (roomCreator > 0 && start >= 0 && length > 0){
            return iApplicationInfo.getUnreadApplicationListByRoomCreator(roomCreator, start, length);
        }
        return null;
    }

    /**
     * 通过创建者的id，获取该创建者未读申请消息的条数
     *
     * @param roomCreator
     * @return
     */
    @Override
    public List<ApplicationInfo> getUnreadCountByRoomCreator(int roomCreator) {
        if (roomCreator > 0){
            return iApplicationInfo.getUnreadCountByRoomCreator(roomCreator);
        }
        return null;
    }

    /**
     * 创建一条新的用户申请者的消息
     *
     * @param applicationInfo
     */
    public void insertApplication(ApplicationInfo applicationInfo) {
        if (applicationInfo.getApplicatorId() > 0
                && applicationInfo.getApplicatorNick() != ""
                && applicationInfo.getContent() != ""
                && applicationInfo.getRoomCreator() > 0
                && applicationInfo.getRoomId() > 0
                && applicationInfo.getRoomName() != ""){
            iApplicationInfo.insertApplication(applicationInfo);
        }

    }

    /**
     * 更新申请消息
     *
     * @param applicationInfo
     */
    public void updateApplication(ApplicationInfo applicationInfo) {
        if ((applicationInfo.getStatus() == ApplicationInfo.STATUS_READED
            || applicationInfo.getStatus() == ApplicationInfo.STATUS_UNREADED)
                &&(applicationInfo.getType() == ApplicationInfo.TYPE_PASS
                    || applicationInfo.getType() == ApplicationInfo.TYPE_REFUSE
                    || applicationInfo.getType() == ApplicationInfo.TYPE_UNKNOW)){
            iApplicationInfo.updateApplication(applicationInfo);
        }

    }

    /**
     * 删除某一申请消息
     *
     * @param applicationInfo
     */
    public void deleteApplication(ApplicationInfo applicationInfo) {
        if (applicationInfo.getId() > 0){
            iApplicationInfo.deleteApplication(applicationInfo);
        }
    }
}
